Imports System.data.oledb
Imports System.IO

Public Class EditFileDetails

    Private _fileId As Integer

    Private Const ImageExistsMessage As String = "Cannot create a file when that file already exists."

    Private Const UserCancelledMessage As String = "The operation was canceled by the user"

    Private Const SingleQuote As String = "'"
    Private Const Apostrophe As String = "`"
    Private Const ListDelimiter As String = ", "

    Private Sub HandleProvider(ByVal providerName As String)

        Select Case providerName
            Case ProviderNames.Daz3D
                If Not Me.FileData.Files(0).Isproduct_linkNull Then
                    If Me.FileData.Files(0).product_link.Trim.Length > 0 Then
                        ProductLink.ReadOnly = True
                    End If
                End If
                DownloadReadMe.Enabled = False
            Case ProviderNames.RuntimeDNA
                If Not Me.FileData.Files(0).Isproduct_linkNull Then
                    If Me.FileData.Files(0).product_link.Trim.Length > 0 Then
                        ProductLink.ReadOnly = True
                    End If
                End If
                DownloadReadMe.Enabled = False
            Case ProviderNames.PoserPros
                DownloadReadMe.Enabled = True
            Case ProviderNames.Renderosity
                DownloadReadMe.Enabled = True
            Case ProviderNames.ThreeDCommune
                DownloadReadMe.Enabled = True
            Case Else
                DownloadReadMe.Enabled = False
        End Select

    End Sub

    Private Sub LoadFiguresForList()

        FiguresForList.Clear()

        Me.FileForFigureTableAdapter.Fill(Me.FileForFigureData.FileForFigure, _
                                          _fileId)

        For Each figure As FileForFigureData.FileForFigureRow In FileForFigureData.FileForFigure

            FiguresForList.Text &= figure.figure
            FiguresForList.Text &= ListDelimiter

        Next

        If FiguresForList.Text.EndsWith(ListDelimiter) Then
            FiguresForList.Text = FiguresForList.Text.Remove(FiguresForList.Text.LastIndexOf(ListDelimiter))
        End If

    End Sub

    Private Sub LoadProductTypesList()

        ProductTypeList.Clear()

        Me.FileProductTypeTableAdapter.Fill(Me.FileProductTypesData.FileProductType, _
                                            _fileId)

        For Each productType As FileProductTypesData.FileProductTypeRow In FileProductTypesData.FileProductType

            ProductTypeList.Text &= productType.product_type
            ProductTypeList.Text &= ListDelimiter

        Next

        If ProductTypeList.Text.EndsWith(ListDelimiter) Then
            ProductTypeList.Text = ProductTypeList.Text.Remove(ProductTypeList.Text.LastIndexOf(ListDelimiter))
        End If

    End Sub

    Private Sub LoadRuntimesList()

        RuntimesList.Clear()

        Me.InstalledRuntimesTableAdapter.Fill(Me.InstalledRuntimesData.InstalledRuntimes, _
                                              _fileId)


        For Each installed As InstalledRuntimesData.InstalledRuntimesRow In InstalledRuntimesData.InstalledRuntimes

            RuntimesList.Text &= installed.runtime
            RuntimesList.Text &= ListDelimiter

        Next

        If RuntimesList.Text.EndsWith(ListDelimiter) Then
            RuntimesList.Text = RuntimesList.Text.Remove(RuntimesList.Text.LastIndexOf(ListDelimiter))
        End If

    End Sub

    Private Sub LoadArtistList()

        ArtistList.Clear()

        Me.FileArtistTableAdapter.Fill(Me.FileArtistData.FileArtist, _
                                       _fileId)


        For Each fileArtist As FileArtistData.FileArtistRow In FileArtistData.FileArtist

            ArtistList.Text &= fileArtist.artist
            ArtistList.Text &= ListDelimiter

        Next

        If ArtistList.Text.EndsWith(ListDelimiter) Then
            ArtistList.Text = ArtistList.Text.Remove(ArtistList.Text.LastIndexOf(ListDelimiter))
        End If

    End Sub

    Private Sub LoadLists()

        LoadFiguresForList()
        LoadProductTypesList()
        LoadRuntimesList()
        LoadArtistList()

    End Sub


    Private Sub UpdateFileDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateFileDetails.Click

        FilesDS.EndEdit()
        'FileImagesDS.EndEdit()

        Dim updated As Integer = Me.FilesTableAdapter.Update(Me.FileData)

        MsgBox("Successfuly updated record.")
        Me.Close()

    End Sub

    Public Sub New(ByVal fileId As Integer)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.LicenseTableAdapter.Fill(Me.LicenseData.License)
        Me.ProviderTableAdapter.Fill(Me.Providers.Provider)

        ' Add any initialization after the InitializeComponent() call.
        _fileId = fileId

        Me.FilesTableAdapter.Fill(Me.FileData.Files, _
                                  fileId)


        If Me.FileData.Files(0).LocalImage And _
           Not Me.FileData.Files(0).IsImageNameNull Then
            AquireImagePanel.Visible = False
            ImageViewer.ImageLocation = My.Settings.ImageDirectory & Me.FileData.Files(0).ImageName
            ImageViewer.Visible = True
            ChangeProductImage.Visible = True
        Else
            AquireImagePanel.Visible = True
            ImageViewer.Visible = False
            ChangeProductImage.Visible = False
        End If

        If Not Me.FileData.Files(0).Isactual_locationNull Then
            If Me.FileData.Files(0).actual_location.Trim.Length > 0 Then
                OpenFile.Enabled = True
            End If
        End If

        HandleProvider(Me.FileData.Files(0).provider)

        LoadLists()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FilesDS.CancelEdit()
        'FileImagesDS.CancelEdit()

        Me.Close()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ImageSelector.ShowDialog()
        Dim file As String = ImageSelector.FileName
        Dim info As New FileInfo(file)
        Try
            Directory.Move(file, _
                           My.Settings.ImageDirectory & info.Name)
            ImageLocation.Text = info.Name
            ImageAttached.Checked = True
            AquireImagePanel.Visible = False
            ImageViewer.ImageLocation = My.Settings.ImageDirectory & info.Name
            ImageViewer.Visible = True
            ChangeProductImage.Visible = True
        Catch ex As IOException
            If ex.Message.Trim = ImageExistsMessage Then
                MsgBox("An image of this name has already been uploaded.")
            Else
                MsgBox(ex.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub DownloadImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DownloadImage.Click

        Dim downloader As BaseDownloader

        Dim vendor As String = Me.FileData.Files(0).provider
        Select Case vendor
            Case ProviderNames.Daz3D
                downloader = DownloaderFactory.GetDownloader(DownloaderFactory.Site.Daz3D, _
                             My.Settings.ImageDirectory, _
                             ImageViewer)
            Case ProviderNames.PoserPros
                downloader = DownloaderFactory.GetDownloader(DownloaderFactory.Site.PoserPros, _
                             My.Settings.ImageDirectory, _
                             ImageViewer)
            Case ProviderNames.Renderosity
                downloader = DownloaderFactory.GetDownloader(DownloaderFactory.Site.Renderosity, _
                             My.Settings.ImageDirectory, _
                             ImageViewer)
            Case ProviderNames.RuntimeDNA
                downloader = DownloaderFactory.GetDownloader(DownloaderFactory.Site.RuntimeDNA, _
                             My.Settings.ImageDirectory, _
                             ImageViewer)
            Case ProviderNames.ThreeDCommune
                downloader = DownloaderFactory.GetDownloader(DownloaderFactory.Site._3DCommune, _
                                                             My.Settings.ImageDirectory, _
                                                             ImageViewer)
            Case Else
                MsgBox("Downloading for this provider is currently not available")
        End Select

        If Not downloader Is Nothing Then
            Cursor = Cursors.WaitCursor
            Try
                ImageLocation.Text = downloader.DownloadImage(Code.Text, _
                                                              ProductLink.Text)
                ImageAttached.Checked = True
                AquireImagePanel.Visible = False
                ImageViewer.Visible = True
                ChangeProductImage.Visible = True
            Catch ex As InvalidProductUrlException
                MsgBox("This file has an invalid product url so an image cannot be downloaded")
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                Cursor = Cursors.Default
            End Try
        End If

    End Sub

    Private Sub ChangeProductImage_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ChangeProductImage.LinkClicked

        ImageAttached.Checked = False
        ImageLocation.Text = ""
        ImageViewer.Visible = False
        ChangeProductImage.Visible = False
        AquireImagePanel.Visible = True

    End Sub


    Private Sub EditFiguresFor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFiguresFor.Click

        Dim eff As New EditFiguresFor(_fileId)
        AddHandler eff.FiguresForChanged, _
                   AddressOf LoadFiguresForList
        eff.ShowDialog()

    End Sub

    Private Sub EditProductType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditProductType.Click

        Dim ept As New EditProductType(_fileId)
        AddHandler ept.ProductTypesChanged, _
                   AddressOf LoadProductTypesList
        ept.ShowDialog()

    End Sub

    Private Sub EditRuntimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditRuntimes.Click

        Dim er As New EditRuntimes(_fileId)
        AddHandler er.InstalledRuntimesChanged, _
                   AddressOf LoadRuntimesList
        er.ShowDialog()

    End Sub

    Private Sub EditArtists_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditArtists.Click

        Dim ea As New EditArtist(_fileId)
        AddHandler ea.ArtistsChanged, _
                   AddressOf LoadArtistList
        ea.ShowDialog()

    End Sub


    Private Sub DownloadReadMe_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles DownloadReadMe.LinkClicked

        Try

            Me.Cursor = Cursors.WaitCursor

            Select Case Provider.Text
                Case ProviderNames.PoserPros
                    ReadMe.Text = PoserProsDownloader.GetReadMe(Code.Text)
                Case ProviderNames.Renderosity
                    ReadMe.Text = RenderosityDownloader.GetReadMe(Code.Text)
                Case ProviderNames.ThreeDCommune
                    ReadMe.Text = ThreeDCommuneDownloader.GetReadMe(Code.Text)
                Case Else
                    'do nothing
            End Select

        Catch ex As Exception

        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub EditFileDetails_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub Provider_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Provider.SelectedIndexChanged

        HandleProvider(Provider.SelectedValue.ToString)

    End Sub

    Private Sub OpenFile_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles OpenFile.LinkClicked

        Try
            System.Diagnostics.Process.Start(Me.FileData.Files(0).actual_location.Replace(Apostrophe, SingleQuote))
        Catch ex As System.ComponentModel.Win32Exception
            If ex.Message <> UserCancelledMessage Then
                MsgBox(ex.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class